package org.jumiz.dbview.own.service;

import org.jumiz.dbview.own.entity.DbType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class DatabasesServiceFactory {

    private static Map<DbType, DatabasesService> dbTypeDatabasesServiceMap = new HashMap<>();

    @Autowired
    public void setDbTypeDatabasesServiceMap(Map<String, DatabasesService> dbTypeDatabasesServiceMap) {
        dbTypeDatabasesServiceMap.entrySet().stream().forEach(service -> {
            if (service.getKey().toUpperCase().startsWith(DbType.H2.name().toUpperCase())) {
                DatabasesServiceFactory.dbTypeDatabasesServiceMap.put(DbType.H2, service.getValue());
            }
        });
        DatabasesServiceFactory.dbTypeDatabasesServiceMap.entrySet().forEach(item -> System.out.println(item.getKey()));
    }

    public static DatabasesService getBean(DbType dbType) {
        return dbTypeDatabasesServiceMap.getOrDefault(dbType, null);
    }
}
